﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HHHFramework {
    public class ResourcesRes : Res
    {
        private string mAssetPath;
        public override bool LoadSync()
        {
            State = ResState.Loading;
            Asset = Resources.Load(mAssetPath, AssetType);
            State = ResState.Loaded;
            return Asset;
        }

        public override void LoadAsync()
        {
          
            State = ResState.Loading;
            //这里的数据必须要继承自system。Object
            var request = Resources.LoadAsync(mAssetPath, AssetType);
            request.completed += operation =>
            {              
                Asset = request.asset;
                State = ResState.Loaded;
            };
        }

        protected override void OnReleaseRes() {
            
            if (Asset is GameObject|| Asset as Component)
            {
                Asset = null;
                Resources.UnloadUnusedAssets();
            }
            else
            {
                Debug.Log(Asset.name);
                Resources.UnloadAsset(Asset);
            }
            ResMgr.Instance.sharedLoadRes.Remove(this);
            Asset = null;
        }
       
        public ResourcesRes(ResSearchKeys resSearchKeys)
        {
            State = ResState.Waiting;
            mAssetName = resSearchKeys.AssetName;
            AssetType = resSearchKeys.AssetType;
            OwnerBundleName = resSearchKeys.OwnerBundle;
            mAssetPath = resSearchKeys.AssetName.Substring("resources://".Length);
        }

    }
}

